/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

import java.util.Vector;
import Complementos.Conexion_BD;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import Modelo.Tratamiento;
/**
 *
 * @author DIEGOALV
 */
public class TratamientoDAO {

        Conexion_BD co=new Conexion_BD();
        ResultSet rs = null;




        public int agregarTratamiento(Tratamiento t){
            int agrego=0;
            try {
            co.Conectar();
            co.insupdel("execute sp_registrar_tratamiento " + "'" + t.getNombre() + "','" + t.getDescripcion() + "','" + t.getCosto() + "'");
            agrego=1;

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
            agrego=2;
        }


        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

                return agrego;


        }


        public boolean eliminarTratamiento(Tratamiento t){

            boolean elimino = false;
        try {
            co.Conectar();
            co.insupdel("execute sp_eliminar_tratamiento" + "'" + t.getNombre() + "'");
            elimino = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
            elimino=false;
        }


        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

            return elimino;
        }



    public Vector<Vector> listarTodos(){

        Vector<Vector> listado = new Vector<Vector>();
        try {
            
            co.Conectar();
            rs = co.Consultar("select nombre,descripcion,costo from TRATAMIENTO");
            if (rs.first()) {
                while (!rs.isAfterLast()) {
                    Vector item = new Vector();
                    item.add(rs.getString(1));
                    item.add(rs.getString(2));
                    item.add(rs.getString(3));
                    //item.add(rs.getString(4));
 
                    //Avanza el RS
                    rs.next();
                    listado.add(item);
                }
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
                    return listado;
                                    }



    public int editarTratamiento(Tratamiento t,String nombre){
       int edito=0;

        try {
            co.Conectar();
            co.insupdel("execute sp_modificar_tratamiento" + "'" + t.getNombre() + "','" + t.getDescripcion() + "','" + t.getCosto() + "','" + nombre + "'");
            edito=1;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(TratamientoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

                    return edito;


    }


}
